﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Enums;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public  class PageForm_TextBoxField: PageForm_SingleInputFieldBase
    {
        //private string _name = "";
        private string _value = "";
        private bool _disabled;
        private string _placeholder = "";
        private bool _readonly = false;
        private int _maxLength = -1;
        private string _minValue = null;
        private string _maxValue = null;
        private bool _isAutoSelect = false;
        private InputElementType _type = InputElementType.text;

        public PageForm_TextBoxField(PageForm_Form form, ViewContext viewContext) : base(form, viewContext)
        {
            this.Css("webui-form_textboxfield");
        }

        public PageForm_TextBoxField Value(string value)
        {
            _value = value;

            return this;
        }
        
        public PageForm_TextBoxField Placeholder(string text)
        {
            _placeholder = text;

            return this;
        }

        public PageForm_TextBoxField ReadOnly(bool isReadOnly = true)
        {
            _readonly = isReadOnly;

            return this;
        }

        public PageForm_TextBoxField MaxLength(int maxLength)
        {
            _maxLength = maxLength;

            return this;
        }

        public PageForm_TextBoxField ValueLimit(int? minValue, int? maxValue = null)
        {
            _minValue = minValue?.ToString();
            _maxValue = maxValue?.ToString();

            return this;
        }

        public PageForm_TextBoxField ValueLimit(decimal? minValue, decimal? maxValue = null)
        {
            _minValue = minValue?.ToString();
            _maxValue = maxValue?.ToString();

            return this;
        }

        public PageForm_TextBoxField SelectedOnClick(bool isAutoSelect = true)
        {
            _isAutoSelect = isAutoSelect;

            return this;
        }

        public PageForm_TextBoxField Type(InputElementType type)
        {
            _type = type;

            return this;
        }

        public PageForm_TextBoxField Disabled(bool disabled = true)
        {
            _disabled = disabled;

            return this;
        }

        protected override void RenderContent(TextWriter writer)
        {
            writer.WriteLine($"<input type=\"{_type}\" name=\"{_name}\"  {(_isRequired ? "required" : "")} data-autoselected=\"{_isAutoSelect}\" value=\"{_value}\" lay-ignore class=\"form-control \" {_readonly.If("readonly")} {(_maxLength>0).If($"maxlength=\"{_maxLength}\"")} placeholder=\"{_placeholder}\" {_disabled.If("disabled")}  {_minValue.IfNotEmptyOrWhileSpace(x=>$"min=\"{x}\"")} {_maxValue.IfNotEmptyOrWhileSpace(x=>$"max=\"{x}\"")}/>");
        }
    }
}
